package com.ssyc.gsk_master.activity;

import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.ssyc.common.base.BaseActivity;
import com.ssyc.common.base.Constants;
import com.ssyc.common.http.HttpAdress;
import com.ssyc.common.http.HttpUtils;
import com.ssyc.common.manager.AccountUtils;
import com.ssyc.common.manager.BqaManager;
import com.ssyc.common.manager.SrfManager;
import com.ssyc.common.utils.GsonUtil;
import com.ssyc.common.utils.UiUtils;
import com.ssyc.gsk_master.R;
import com.ssyc.gsk_master.adapter.RedListRvAdapter;
import com.ssyc.gsk_master.bean.RedListInfo;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;

/**
 * 红包记录
 * Created by Administrator on 2018/7/23 0023.
 */

public class MasterRedPackageListActivity extends BaseActivity implements View.OnClickListener, SrfManager.OnPullRefreshListener {

    private ImageView ivBack;
    private RecyclerView rv;
    private SmartRefreshLayout srf;
    private RelativeLayout rlLoadingView;
    //内容布局
    private RelativeLayout rlConent;

    //总发出,总领取人数,总使用
    private TextView tvTotalSend;
    private TextView tvTotalPeople;
    private TextView tvTotalUse;

    //空视图
    private View emptyView;


    //数据集合
    private List<RedListInfo.RedPacketsBean> oldDatas;

    //适配器
    private RedListRvAdapter adapter;

    @Override
    protected boolean isNeedShowTitle() {
        return false;
    }

    @Override
    protected String setTitleText() {
        return null;
    }

    @Override
    protected int getContentLayout() {
        return R.layout.master_activity_redpackage_list;
    }

    @Override
    protected void init() {

        showContent();

        //初始化视图
        initView();

        //初始化下拉刷新控件
        initSrf();

        //设置数据
        initRv();

        //请求数据
        http(BqaManager.NORML);
    }


    /**
     * 初始化视图
     */
    private void initView() {
        ivBack = findViewById(R.id.iv_back);
        ivBack.setOnClickListener(this);
        rv = findViewById(R.id.rv);
        srf = findViewById(R.id.srf);
        rlLoadingView = findViewById(R.id.rl_loading);
        rlLoadingView.setVisibility(View.VISIBLE);
        rlConent = findViewById(R.id.rl_content);
        rlConent.setVisibility(View.GONE);
        tvTotalSend = findViewById(R.id.tv_total_send);
        tvTotalPeople = findViewById(R.id.tv_total_people);
        tvTotalUse = findViewById(R.id.tv_total_used);
    }

    /**
     * 初始化下拉刷新控件
     */
    private void initSrf() {
        SrfManager.setSmartByPullListener(srf, this);
    }

    /**
     * 设置列表
     */
    private void initRv() {
        oldDatas = new ArrayList<>();
        emptyView = View.inflate(this, R.layout.base_rv_common_empty_view, null);
        TextView tvMsg = emptyView.findViewById(R.id.tv_content);
        tvMsg.setText("暂无红包记录");
        adapter = new RedListRvAdapter(this, R.layout.master_rv_redpackage_list, oldDatas);
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                // 跳转到红包领取记录界面
                Intent intent = new Intent(MasterRedPackageListActivity.this, MasterRedPackageDetailsActivity.class);
                intent.putExtra("redid", oldDatas.get(position).getRedId() + "");
                //总金额
                intent.putExtra("totalmoney", oldDatas.get(position).getMoney() + "");
                //总领取人数
                intent.putExtra("totalperson", oldDatas.get(position).getRobbedPerson() + "");
                //总使用
                intent.putExtra("totaluse", oldDatas.get(position).getUsedMoney() + "");
                startActivity(intent);
            }
        });
    }

    /**
     * 请求服务器数据的方法
     *
     * @param method
     */
    private void http(int method) {

        Map<String, String> map = new HashMap<>();
        map.put("type", "6");
        map.put("schoolId", AccountUtils.getSchoolId(this));
        map.put("c", AccountUtils.getAccount(this));
        map.put("role", AccountUtils.getRole(this));
        map.put("platform", "8");

        HttpUtils.post(HttpAdress.MASTERREDLIST, map, this, new HttpUtils.CallBack() {
            @Override
            public void onError(Call call, Exception e, int id) {
                BqaManager.setErrorNet(rlLoadingView, srf, method, adapter);
            }

            @Override
            public void onResponse(String response, int id) {
                if (rlLoadingView != null && rlLoadingView.getVisibility() == View.VISIBLE) {
                    rlLoadingView.setVisibility(View.GONE);
                }

                if (rlConent != null && rlConent.getVisibility() != View.VISIBLE) {
                    rlConent.setVisibility(View.VISIBLE);
                }

                if (!TextUtils.isEmpty(response)) {
                    RedListInfo info = null;
                    try {
                        info = GsonUtil.jsonToBean(response, RedListInfo.class);
                    } catch (Exception e) {
                        Log.i("test", Constants.PARSEDATAERROR);
                    }
                    if (null != info && "1".equals(info.getState())) {
                        //设置顶部的统计数据
                        setTopData(info);
                        //返回成功
                        List<RedListInfo.RedPacketsBean> newDatas = info.getRedPackets();
                        Collections.reverse(newDatas);

                        if (newDatas != null) {
                            if (method == BqaManager.NORML) {
                                oldDatas.clear();
                                oldDatas.addAll(newDatas);
                                BqaManager.setRv(emptyView, MasterRedPackageListActivity.this, adapter, rv);
                            } else {
                                //更新数据
                                BqaManager.updateRvBySmf(method, srf, rv, oldDatas, newDatas, adapter);
                                adapter.setEnableLoadMore(false);
                            }
                        }
                    } else if ("-1".equals(info.getState())){
                        oldDatas.addAll(new ArrayList<RedListInfo.RedPacketsBean>());
                        BqaManager.setRv(emptyView, MasterRedPackageListActivity.this, adapter, rv);

                    } else{

                        UiUtils.Toast(Constants.ERRORSTATE + info.getState(), false);
                        Log.i("test", Constants.ERRORSTATE + info.getState());
                    }
                } else {
                    Log.i("test", Constants.EMPTY);
                }
            }
        });
    }


    /**
     * 设置总发出,总领取人数,总使用的数据
     *
     * @param info
     */
    private void setTopData(RedListInfo info) {
        tvTotalSend.setText("￥" + info.getTotleMoney());
        tvTotalPeople.setText(info.getTotleRobbed() + "人");
        tvTotalUse.setText("￥" + info.getTotleUsed());
    }


    /**
     * 下拉刷新
     *
     * @param refreshlayout
     */
    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        http(BqaManager.PULLREFRESH);
    }

    /**
     * 点击事件
     *
     * @param view
     */
    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.iv_back) {
            //返回
            finish();
        }
    }
}
